<?php

namespace App\Http\Controllers\Admin\ContentManager;

use App\Http\Controllers\BaseController;
use App\Services\Tools\CommonService;
use App\Services\Tools\ExportService;
use Carbon\Carbon;
use Illuminate\Http\Request;    //引用http请求类
use App\Exceptions\BasicException;  //引入框架异常处理类
use Illuminate\Support\Facades\Hash;    //引入hash
use App\Services\Cache\AdminCacheService;   //admin缓存
use App\Services\Tools\JwtService;    //引入JWT服务
use App\Services\Tools\FileService;   //引入文件处理类
use App\Response\Response;
use App\Models\MainDB\GiveGiftDay;
use Illuminate\Support\Facades\Redis;

class ChatLogController extends BaseController {


    /**
     * @api {get} /content-manager/chat-log 聊天记录
     * @apiVersion 0.1.0
     * @apiName  内容管理-聊天记录
     * @apiGroup 内容管理
     *
     * @apiParam {number} type         1：群聊 2：私聊 必传
     * @apiParam {string} date_start   开始日期,非必传
     * @apiParam {string} date_end     结束日期，非必传
     * @apiParam {string} room_name    房间名称，非必传
     * @apiParam {string} content      聊天内容，非必传
     * @apiParam {string} nickname     昵称，非必传
     * @apiParam {number} user_code    用户编号，非必传
     * @apiParam {string} target_nickname     对方昵称，非必传
     * @apiParam {number} target_user_code    对方用户编号，非必传
     * @apiSuccessExample 成功响应
     *{
    "code": 200,
    "data": {
    "current_page": 1,
    "data": [
    {
    "user_id": 1296271,
    "user_id_to": 1296288,
    "room_id": 20443,
    "content": "在学校", 消息内容
    "type": 1,
    "add_time": 1623135512,
    "chat_time": "2021-06-08 14:58:32", 消息时间
    "room_name": "哈哈", 房间名称（type=1时有返回）
    "target_user_code": 5586528, 对方用户编号（type=2时有返回）
    "target_nickname": "巫马从霜558652", 对方昵称（type=2时有返回）
    "target_head_picture": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/avatar/10.jpg", 对方头像（type=2时有返回）
    "user_code": 4656276, 用户编号
    "nickname": "滦青香4656276", 昵称
    "head_picture": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/avatar/6.jpg" 头像
    }
    ],
    "first_page_url": "http://local.qipaoadmin/content-manager/chat-log?page=1",
    "from": 1,
    "last_page": 7,
    "last_page_url": "http://local.qipaoadmin/content-manager/chat-log?page=7",
    "next_page_url": "http://local.qipaoadmin/content-manager/chat-log?page=2",
    "path": "http://local.qipaoadmin/content-manager/chat-log",
    "per_page": "10",
    "prev_page_url": null,
    "to": 10,
    "total": 68
    }
    }
     *
     */
    public function list(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(), [
            'date_start' => 'string|date_format:Y-m-d',
            'date_end' => 'string|date_format:Y-m-d',
            'perPage' => 'numeric',
            'page' => 'numeric',
            'type' => 'required|numeric|in:1,2',
            'room_name' => 'string',
            'content' => 'string',
            'nickname' => 'string',
            "user_code" => 'numeric',
            'target_nickname' => 'string',
            "target_user_code" => 'numeric',
            'user_type' => 'filled|integer|in:0,1,2,3',//己方身份类别0普通人1会长2厅主3主播
            'user_to_type' => 'filled|integer|in:0,1,2,3',//对方身份类别0普通人1会长2厅主3主播
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $list = app('repo_content')->getChatLogQueryList($request->input());

        return Response::sendData($list);
    }


    /**
     * @api {get} /content-manager/chat-log/export/excel 聊天记录-导出
     * @apiVersion 0.1.0
     * @apiName  内容管理-聊天记录-导出
     * @apiGroup 内容管理
     *
     * @apiParam {number} type         1：群聊 2：私聊 必传
     * @apiParam {string} date_start   开始日期,非必传
     * @apiParam {string} date_end     结束日期，非必传
     * @apiParam {string} room_name    房间名称，非必传
     * @apiParam {string} content      聊天内容，非必传
     * @apiParam {string} nickname     昵称，非必传
     * @apiParam {number} user_code    用户编号，非必传
     * @apiParam {string} target_nickname     对方昵称，非必传
     * @apiParam {number} target_user_code    对方用户编号，非必传
     * @apiSuccessExample 成功响应
        {
        "code": 200,
        "data": {
            "download_url": "asdasdasd.com"
        }
     *
     */
    public function export(Request $request){
        //参数校验
        try {
            $this->validate($request, [
                'date_start' => 'filled|string|date_format:Y-m-d',
                'date_end' => 'filled|string|date_format:Y-m-d',
                'perPage' => 'filled|numeric',
                'type' => 'required|numeric',
                'page' => 'filled|numeric',
                'room_name' => 'filled|string',
                'content' => 'filled|string',
                'nickname' => 'filled|string',
                "user_code" => 'filled|numeric',
                'target_nickname' => 'filled|string',
                "target_user_code" => 'filled|numeric',
                'user_type' => 'filled|integer|in:0,1,2,3',//己方身份类别0普通人1会长2厅主3主播
                'user_to_type' => 'filled|integer|in:0,1,2,3',//对方身份类别0普通人1会长2厅主3主播
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }
        $params = $request->input();
        $params['perPage'] = 10000;
        $list = app('repo_content')->getChatLogQueryList($params);

        $result = [];
        if ($list){

            foreach ($list['data'] as $item) {
                switch ($item['user_type']) {
                    case 0:
                        $userType = '普通用户';
                        break;
                    case 1:
                        $userType = '公会会长';
                        break;
                    case 2:
                        $userType = '公会厅主';
                        break;
                    case 3:
                        $userType = '公会成员';
                        break;
                }
                $temp = [
                    $item['user_code'] ?? '',
                    app('repo_export')->filter_emoji($item['nickname'] ?? ''),
                    $userType ?? '',
                    app('repo_export')->filter_emoji($item['content'] ?? ''),
                    $item['chat_time'] ?? '',
                ];

                if ($params['type'] == 1) {
                    $temp[] = app('repo_export')->filter_emoji($item['room_name'] ?? '');
                } else {
                    switch ($item['user_to_type']) {
                        case 0:
                            $userToType = '普通用户';
                            break;
                        case 1:
                            $userToType = '公会会长';
                            break;
                        case 2:
                            $userToType = '公会厅主';
                            break;
                        case 3:
                            $userToType = '公会成员';
                            break;
                    }
                    $temp[] = $item['target_user_code'] ?? '';
                    $temp[] = app('repo_export')->filter_emoji($item['target_nickname'] ?? '');
                    $temp[] = $userToType ?? '';
                }

                $result[] = $temp;
            }
        }

        if ($params['type'] == 1) {
            $headings = [
                '用户编号',
                '昵称',
                '用户类型',
                '消息内容',
                '消息时间',
                '房间名称',
            ];
        } else {
            $headings = [
                '用户编号',
                '昵称',
                '用户类型',
                '消息内容',
                '消息时间',
                '对方用户编号',
                "对方昵称",
                '对方用户类型',
            ];
        }

        return app('repo_export')->export(5, $params, $headings, $result);
    }
}
